package ocean.chinacraft.common.world.item.armor;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.IArmorMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import ocean.chinacraft.common.constants.Constants;
import ocean.chinacraft.common.entity.goal.MonsterAttackGoal;
import ocean.chinacraft.common.entity.goal.MonsterLookAtGoal;
import ocean.chinacraft.common.world.item.armor.base.ArmorMaterial;
import ocean.chinacraft.common.util.ModUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.util.List;

/**
 * 夜行衣全套效果
 *
 * @author zhang
 * @since 2021/12/5
 */
public class SilkNightArmor extends ArmorItem {

    protected static final Logger LOGGER = LogManager.getLogger();

    public SilkNightArmor(ArmorMaterial material, EquipmentSlotType slotType, Properties properties) {
        super(material, slotType, properties);
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void appendHoverText(ItemStack itemStack, @Nullable World world, List<ITextComponent> textComponents,
                                ITooltipFlag tipFlag) {
        textComponents.add(new TranslationTextComponent("tooltip.chinacraft.nightarmor.0"));
        textComponents.add(new TranslationTextComponent("tooltip.chinacraft.nightarmor.1"));
        textComponents.add(new TranslationTextComponent("tooltip.chinacraft.nightarmor.2"));
        textComponents.add(new TranslationTextComponent("tooltip.chinacraft.nightarmor.3"));
    }

    @Mod.EventBusSubscriber(modid = Constants.MOD_ID)
    public static class RenderNightArmor {

        @OnlyIn(Dist.CLIENT)
        @SubscribeEvent
        public static void onPreRenderPlayer(final RenderPlayerEvent.Pre event) {
            if (invalid(event.getPlayer())) {
                event.setCanceled(true);
            }
        }

        @SubscribeEvent
        public static void onEntityJoinWorldEvent(final EntityJoinWorldEvent event) {
            Entity entity = event.getEntity();
            if (entity.isAlive() && entity instanceof MobEntity) {
                MobEntity mobEntity = (MobEntity) entity;
                GoalSelector targetSelector = mobEntity.targetSelector;
                targetSelector.addGoal(1, new MonsterAttackGoal(mobEntity, false));
                targetSelector.addGoal(1, new MonsterLookAtGoal(mobEntity, PlayerEntity.class, 8F));
            }
        }
    }

    private static boolean hasCorrectArmorOn(IArmorMaterial material, PlayerEntity player) {
        try {
            NonNullList<ItemStack> armorList = player.inventory.armor;
            ItemStack boot = armorList.get(0);
            ItemStack leggings = armorList.get(1);
            ItemStack chest = armorList.get(2);
            ItemStack helmet = armorList.get(3);
            if (ModUtil.isNotNull(boot, leggings, chest, helmet)) {
                ArmorItem b = (ArmorItem) boot.getItem();
                ArmorItem l = (ArmorItem) leggings.getItem();
                ArmorItem c = (ArmorItem) chest.getItem();
                ArmorItem h = (ArmorItem) helmet.getItem();
                return h.getMaterial() == material && c.getMaterial() == material &&
                        l.getMaterial() == material && b.getMaterial() == material;
            }
        } catch (Exception e) {
            LOGGER.debug("似乎不是一个Armor，跳过此错误...");
        }
        return false;
    }

    public static boolean hasFood(PlayerEntity player) {
        return player.getFoodData().getFoodLevel() >= 14;
    }

    public static boolean invalid(PlayerEntity player) {
        return hasCorrectArmorOn(ArmorMaterial.SILK_NIGHT_ARMOR, player) && hasFood(player) && player.isShiftKeyDown();
    }
}
